package com.mapreduce;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReduce extends Reducer<Text, LongWritable, Text, LongWritable>{

	/**
	 * reduce方法是每获得一个<key,valuelist>,执行一次
	 */
	//key：某一个单词，比如hello  values：这个单词的所有v，封装在一个迭代器中，可以理解为一个list(1,1,1,...)
	@Override
	protected void reduce(Text key, Iterable<LongWritable> values,Context context)
			throws IOException, InterruptedException {
		long count = 0;
		//遍历该Key所有的valuelist，将所有value累加到计数器中
		for(LongWritable value:values){
			count += value.get();
		}
		
		context.write(key,new LongWritable(count));
	}
	
}
